use std::ops::Add;
use std::marker::PhantomData;

// 定义空枚举 定义单元类型
#[derive(Debug, Clone, Copy)]
enum Inch {}

#[derive(Debug, Clone, Copy)]
enum Mm {}

// 定义带有单位的数字类型
#[derive(Debug, Clone, Copy)]
struct Length<Unit>(f64, PhantomData<Unit>);

// 定义+ 法操作符的实现
impl<Unit> Add for Length<Unit> {
    type Output = Length<Unit>;

    fn add(self, rhs: Length<Unit>) -> Length<Unit> {
        Length(self.0 + rhs.0, PhantomData)
    }
}

fn main() {

    let one_foot: Length<Inch> = Length(12.0, PhantomData);

    let one_meter: Length<Mm> = Length(1000.0, PhantomData);

    // + calls the add method defined above implemented for Length<Unit>
    let two_feet = one_foot + one_foot;
    let three_meters = one_meter + one_meter + one_meter;

    println!("one foot + one foot = {:?}", two_feet.0);
    println!("one meter + one meter + one meter = {:?}", three_meters.0);
}